home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / specfunc / zeta.c < prev   
Encoding:
C/C++ Source or Header  |  2002-04-18  |  25.2 KB  |  788 lines

  1. /* specfunc/zeta.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Author:  G. Jungman */
  21.  
  22. #include <config.h>
  23. #include <gsl/gsl_math.h>
  24. #include <gsl/gsl_errno.h>
  25. #include <gsl/gsl_sf_elementary.h>
  26. #include <gsl/gsl_sf_exp.h>
  27. #include <gsl/gsl_sf_gamma.h>
  28. #include <gsl/gsl_sf_pow_int.h>
  29. #include <gsl/gsl_sf_zeta.h>
  30.  
  31. #include "error.h"
  32.  
  33. #include "chebyshev.h"
  34. #include "cheb_eval.c"
  35.  
  36. #define LogTwoPi_  1.8378770664093454835606594728111235279723
  37.  
  38.  
  39. /*-*-*-*-*-*-*-*-*-*-*-* Private Section *-*-*-*-*-*-*-*-*-*-*-*/
  40.  
  41. /* chebyshev fit for (s(t)-1)Zeta[s(t)]
  42.  * s(t)= (t+1)/2
  43.  * -1 <= t <= 1
  44.  */
  45. static double zeta_xlt1_data[14] = {
  46.   1.48018677156931561235192914649,
  47.   0.25012062539889426471999938167,
  48.   0.00991137502135360774243761467,
  49.  -0.00012084759656676410329833091,
  50.  -4.7585866367662556504652535281e-06,
  51.   2.2229946694466391855561441361e-07,
  52.  -2.2237496498030257121309056582e-09,
  53.  -1.0173226513229028319420799028e-10,
  54.   4.3756643450424558284466248449e-12,
  55.  -6.2229632593100551465504090814e-14,
  56.  -6.6116201003272207115277520305e-16,
  57.   4.9477279533373912324518463830e-17,
  58.  -1.0429819093456189719660003522e-18,
  59.   6.9925216166580021051464412040e-21,
  60. };
  61. static cheb_series zeta_xlt1_cs = {
  62.   zeta_xlt1_data,
  63.   13,
  64.   -1, 1,
  65.   8
  66. };
  67.  
  68. /* chebyshev fit for (s(t)-1)Zeta[s(t)]
  69.  * s(t)= (19t+21)/2
  70.  * -1 <= t <= 1
  71.  */
  72. static double zeta_xgt1_data[30] = {
  73.   19.3918515726724119415911269006,
  74.    9.1525329692510756181581271500,
  75.    0.2427897658867379985365270155,
  76.   -0.1339000688262027338316641329,
  77.    0.0577827064065028595578410202,
  78.   -0.0187625983754002298566409700,
  79.    0.0039403014258320354840823803,
  80.   -0.0000581508273158127963598882,
  81.   -0.0003756148907214820704594549,
  82.    0.0001892530548109214349092999,
  83.   -0.0000549032199695513496115090,
  84.    8.7086484008939038610413331863e-6,
  85.    6.4609477924811889068410083425e-7,
  86.   -9.6749773915059089205835337136e-7,
  87.    3.6585400766767257736982342461e-7,
  88.   -8.4592516427275164351876072573e-8,
  89.    9.9956786144497936572288988883e-9,
  90.    1.4260036420951118112457144842e-9,
  91.   -1.1761968823382879195380320948e-9,
  92.    3.7114575899785204664648987295e-10,
  93.   -7.4756855194210961661210215325e-11,
  94.    7.8536934209183700456512982968e-12,
  95.    9.9827182259685539619810406271e-13,
  96.   -7.5276687030192221587850302453e-13,
  97.    2.1955026393964279988917878654e-13,
  98.   -4.1934859852834647427576319246e-14,
  99.    4.6341149635933550715779074274e-15,
  100.    2.3742488509048340106830309402e-16,
  101.   -2.7276516388124786119323824391e-16,
  102.    7.8473570134636044722154797225e-17
  103. };
  104. static cheb_series zeta_xgt1_cs = {
  105.   zeta_xgt1_data,
  106.   29,
  107.   -1, 1,
  108.   17
  109. };
  110.  
  111.  
  112. /* assumes s >= 0 and s != 1.0 */
  113. inline
  114. static int
  115. riemann_zeta_sgt0(double s, gsl_sf_result * result)
  116. {
  117.   if(s < 1.0) {
  118.     gsl_sf_result c;
  119.     cheb_eval_e(&zeta_xlt1_cs, 2.0*s - 1.0, &c);
  120.     result->val = c.val / (s - 1.0);
  121.     result->err = c.err / fabs(s-1.0) + GSL_DBL_EPSILON * fabs(result->val);
  122.     return GSL_SUCCESS;
  123.   }
  124.   else if(s <= 20.0) {
  125.     double x = (2.0*s - 21.0)/19.0;
  126.     gsl_sf_result c;
  127.     cheb_eval_e(&zeta_xgt1_cs, x, &c);
  128.     result->val = c.val / (s - 1.0);
  129.     result->err = c.err / (s - 1.0) + GSL_DBL_EPSILON * fabs(result->val);
  130.     return GSL_SUCCESS;
  131.   }
  132.   else {
  133.     double f2 = 1.0 - pow(2.0,-s);
  134.     double f3 = 1.0 - pow(3.0,-s);
  135.     double f5 = 1.0 - pow(5.0,-s);
  136.     double f7 = 1.0 - pow(7.0,-s);
  137.     result->val = 1.0/(f2*f3*f5*f7);
  138.     result->err = 3.0 * GSL_DBL_EPSILON * fabs(result->val);
  139.     return GSL_SUCCESS;
  140.   }
  141. }
  142.  
  143. inline
  144. static int
  145. riemann_zeta1m_slt0(double s, gsl_sf_result * result)
  146. {
  147.   if(s > -19.0) {
  148.     double x = (-19 - 2.0*s)/19.0;
  149.     gsl_sf_result c;
  150.     cheb_eval_e(&zeta_xgt1_cs, x, &c);
  151.     result->val = c.val / (-s);
  152.     result->err = c.err / (-s) + GSL_DBL_EPSILON * fabs(result->val);
  153.     return GSL_SUCCESS;
  154.   }
  155.   else {
  156.     double f2 = 1.0 - pow(2.0,-(1.0-s));
  157.     double f3 = 1.0 - pow(3.0,-(1.0-s));
  158.     double f5 = 1.0 - pow(5.0,-(1.0-s));
  159.     double f7 = 1.0 - pow(7.0,-(1.0-s));
  160.     result->val = 1.0/(f2*f3*f5*f7);
  161.     result->err = 3.0 * GSL_DBL_EPSILON * fabs(result->val);
  162.     return GSL_SUCCESS;
  163.   }
  164. }
  165.  
  166. /* zeta(n) */
  167. #define ZETA_POS_TABLE_NMAX   100
  168. static double zeta_pos_int_table[ZETA_POS_TABLE_NMAX+1] = {
  169.  -0.50000000000000000000000000000,       /* zeta(0) */
  170.   0.0 /* FIXME: DirectedInfinity() */,   /* zeta(1) */
  171.   1.64493406684822643647241516665,       /* ...     */
  172.   1.20205690315959428539973816151,
  173.   1.08232323371113819151600369654,
  174.   1.03692775514336992633136548646,
  175.   1.01734306198444913971451792979,
  176.   1.00834927738192282683979754985,
  177.   1.00407735619794433937868523851,
  178.   1.00200839282608221441785276923,
  179.   1.00099457512781808533714595890,
  180.   1.00049418860411946455870228253,
  181.   1.00024608655330804829863799805,
  182.   1.00012271334757848914675183653,
  183.   1.00006124813505870482925854511,
  184.   1.00003058823630702049355172851,
  185.   1.00001528225940865187173257149,
  186.   1.00000763719763789976227360029,
  187.   1.00000381729326499983985646164,
  188.   1.00000190821271655393892565696,
  189.   1.00000095396203387279611315204,
  190.   1.00000047693298678780646311672,
  191.   1.00000023845050272773299000365,
  192.   1.00000011921992596531107306779,
  193.   1.00000005960818905125947961244,
  194.   1.00000002980350351465228018606,
  195.   1.00000001490155482836504123466,
  196.   1.00000000745071178983542949198,
  197.   1.00000000372533402478845705482,
  198.   1.00000000186265972351304900640,
  199.   1.00000000093132743241966818287,
  200.   1.00000000046566290650337840730,
  201.   1.00000000023283118336765054920,
  202.   1.00000000011641550172700519776,
  203.   1.00000000005820772087902700889,
  204.   1.00000000002910385044497099687,
  205.   1.00000000001455192189104198424,
  206.   1.00000000000727595983505748101,
  207.   1.00000000000363797954737865119,
  208.   1.00000000000181898965030706595,
  209.   1.00000000000090949478402638893,
  210.   1.00000000000045474737830421540,
  211.   1.00000000000022737368458246525,
  212.   1.00000000000011368684076802278,
  213.   1.00000000000005684341987627586,
  214.   1.00000000000002842170976889302,
  215.   1.00000000000001421085482803161,
  216.   1.00000000000000710542739521085,
  217.   1.00000000000000355271369133711,
  218.   1.00000000000000177635684357912,
  219.   1.00000000000000088817842109308,
  220.   1.00000000000000044408921031438,
  221.   1.00000000000000022204460507980,
  222.   1.00000000000000011102230251411,
  223.   1.00000000000000005551115124845,
  224.   1.00000000000000002775557562136,
  225.   1.00000000000000001387778780973,
  226.   1.00000000000000000693889390454,
  227.   1.00000000000000000346944695217,
  228.   1.00000000000000000173472347605,
  229.   1.00000000000000000086736173801,
  230.   1.00000000000000000043368086900,
  231.   1.00000000000000000021684043450,
  232.   1.00000000000000000010842021725,
  233.   1.00000000000000000005421010862,
  234.   1.00000000000000000002710505431,
  235.   1.00000000000000000001355252716,
  236.   1.00000000000000000000677626358,
  237.   1.00000000000000000000338813179,
  238.   1.00000000000000000000169406589,
  239.   1.00000000000000000000084703295,
  240.   1.00000000000000000000042351647,
  241.   1.00000000000000000000021175824,
  242.   1.00000000000000000000010587912,
  243.   1.00000000000000000000005293956,
  244.   1.00000000000000000000002646978,
  245.   1.00000000000000000000001323489,
  246.   1.00000000000000000000000661744,
  247.   1.00000000000000000000000330872,
  248.   1.00000000000000000000000165436,
  249.   1.00000000000000000000000082718,
  250.   1.00000000000000000000000041359,
  251.   1.00000000000000000000000020680,
  252.   1.00000000000000000000000010340,
  253.   1.00000000000000000000000005170,
  254.   1.00000000000000000000000002585,
  255.   1.00000000000000000000000001292,
  256.   1.00000000000000000000000000646,
  257.   1.00000000000000000000000000323,
  258.   1.00000000000000000000000000162,
  259.   1.00000000000000000000000000081,
  260.   1.00000000000000000000000000040,
  261.   1.00000000000000000000000000020,
  262.   1.00000000000000000000000000010,
  263.   1.00000000000000000000000000005,
  264.   1.00000000000000000000000000003,
  265.   1.00000000000000000000000000001,
  266.   1.00000000000000000000000000001,
  267.   1.00000000000000000000000000000,
  268.   1.00000000000000000000000000000,
  269.   1.00000000000000000000000000000
  270. };
  271. #define ZETA_NEG_TABLE_NMAX  99
  272. #define ZETA_NEG_TABLE_SIZE  50
  273. static double zeta_neg_int_table[ZETA_NEG_TABLE_SIZE] = {
  274.  -0.083333333333333333333333333333,     /* zeta(-1) */
  275.   0.008333333333333333333333333333,     /* zeta(-3) */
  276.  -0.003968253968253968253968253968,     /* ...      */
  277.   0.004166666666666666666666666667,
  278.  -0.007575757575757575757575757576,
  279.   0.021092796092796092796092796093,
  280.  -0.083333333333333333333333333333,
  281.   0.44325980392156862745098039216,
  282.  -3.05395433027011974380395433027,
  283.   26.4562121212121212121212121212,
  284.  -281.460144927536231884057971014,
  285.   3607.5105463980463980463980464,
  286.  -54827.583333333333333333333333,
  287.   974936.82385057471264367816092,
  288.  -2.0052695796688078946143462272e+07,
  289.   4.7238486772162990196078431373e+08,
  290.  -1.2635724795916666666666666667e+10,
  291.   3.8087931125245368811553022079e+11,
  292.  -1.2850850499305083333333333333e+13,
  293.   4.8241448354850170371581670362e+14,
  294.  -2.0040310656516252738108421663e+16,
  295.   9.1677436031953307756992753623e+17,
  296.  -4.5979888343656503490437943262e+19,
  297.   2.5180471921451095697089023320e+21,
  298.  -1.5001733492153928733711440151e+23,
  299.   9.6899578874635940656497942895e+24,
  300.  -6.7645882379292820990945242302e+26,
  301.   5.0890659468662289689766332916e+28,
  302.  -4.1147288792557978697665486068e+30,
  303.   3.5666582095375556109684574609e+32,
  304.  -3.3066089876577576725680214670e+34,
  305.   3.2715634236478716264211227016e+36,
  306.  -3.4473782558278053878256455080e+38,
  307.   3.8614279832705258893092720200e+40,
  308.  -4.5892974432454332168863989006e+42,
  309.   5.7775386342770431824884825688e+44,
  310.  -7.6919858759507135167410075972e+46,
  311.   1.0813635449971654696354033351e+49,
  312.  -1.6029364522008965406067102346e+51,
  313.   2.5019479041560462843656661499e+53,
  314.  -4.1067052335810212479752045004e+55,
  315.   7.0798774408494580617452972433e+57,
  316.  -1.2804546887939508790190849756e+60,
  317.   2.4267340392333524078020892067e+62,
  318.  -4.8143218874045769355129570066e+64,
  319.   9.9875574175727530680652777408e+66,
  320.  -2.1645634868435185631335136160e+69,
  321.   4.8962327039620553206849224516e+71,    /* ...        */
  322.  -1.1549023923963519663954271692e+74,    /* zeta(-97)  */
  323.   2.8382249570693706959264156336e+76     /* zeta(-99)  */
  324. };
  325.  
  326.  
  327. /* coefficients for Maclaurin summation in hzeta()
  328.  * B_{2j}/(2j)!
  329.  */
  330. static double hzeta_c[15] = {
  331.   1.00000000000000000000000000000,
  332.   0.083333333333333333333333333333,
  333.  -0.00138888888888888888888888888889,
  334.   0.000033068783068783068783068783069,
  335.  -8.2671957671957671957671957672e-07,
  336.   2.0876756987868098979210090321e-08,
  337.  -5.2841901386874931848476822022e-10,
  338.   1.3382536530684678832826980975e-11,
  339.  -3.3896802963225828668301953912e-13,
  340.   8.5860620562778445641359054504e-15,
  341.  -2.1748686985580618730415164239e-16,
  342.   5.5090028283602295152026526089e-18,
  343.  -1.3954464685812523340707686264e-19,
  344.   3.5347070396294674716932299778e-21,
  345.  -8.9535174270375468504026113181e-23
  346. };
  347.  
  348. #define ETA_POS_TABLE_NMAX  100
  349. static double eta_pos_int_table[ETA_POS_TABLE_NMAX+1] = {
  350. 0.50000000000000000000000000000,  /* eta(0) */
  351. M_LN2,                            /* eta(1) */
  352. 0.82246703342411321823620758332,  /* ...    */
  353. 0.90154267736969571404980362113,
  354. 0.94703282949724591757650323447,
  355. 0.97211977044690930593565514355,
  356. 0.98555109129743510409843924448,
  357. 0.99259381992283028267042571313,
  358. 0.99623300185264789922728926008,
  359. 0.99809429754160533076778303185,
  360. 0.99903950759827156563922184570,
  361. 0.99951714349806075414409417483,
  362. 0.99975768514385819085317967871,
  363. 0.99987854276326511549217499282,
  364. 0.99993917034597971817095419226,
  365. 0.99996955121309923808263293263,
  366. 0.99998476421490610644168277496,
  367. 0.99999237829204101197693787224,
  368. 0.99999618786961011347968922641,
  369. 0.99999809350817167510685649297,
  370. 0.99999904661158152211505084256,
  371. 0.99999952325821554281631666433,
  372. 0.99999976161323082254789720494,
  373. 0.99999988080131843950322382485,
  374. 0.99999994039889239462836140314,
  375. 0.99999997019885696283441513311,
  376. 0.99999998509923199656878766181,
  377. 0.99999999254955048496351585274,
  378. 0.99999999627475340010872752767,
  379. 0.99999999813736941811218674656,
  380. 0.99999999906868228145397862728,
  381. 0.99999999953434033145421751469,
  382. 0.99999999976716989595149082282,
  383. 0.99999999988358485804603047265,
  384. 0.99999999994179239904531592388,
  385. 0.99999999997089618952980952258,
  386. 0.99999999998544809143388476396,
  387. 0.99999999999272404460658475006,
  388. 0.99999999999636202193316875550,
  389. 0.99999999999818101084320873555,
  390. 0.99999999999909050538047887809,
  391. 0.99999999999954525267653087357,
  392. 0.99999999999977262633369589773,
  393. 0.99999999999988631316532476488,
  394. 0.99999999999994315658215465336,
  395. 0.99999999999997157829090808339,
  396. 0.99999999999998578914539762720,
  397. 0.99999999999999289457268000875,
  398. 0.99999999999999644728633373609,
  399. 0.99999999999999822364316477861,
  400. 0.99999999999999911182158169283,
  401. 0.99999999999999955591079061426,
  402. 0.99999999999999977795539522974,
  403. 0.99999999999999988897769758908,
  404. 0.99999999999999994448884878594,
  405. 0.99999999999999997224442439010,
  406. 0.99999999999999998612221219410,
  407. 0.99999999999999999306110609673,
  408. 0.99999999999999999653055304826,
  409. 0.99999999999999999826527652409,
  410. 0.99999999999999999913263826204,
  411. 0.99999999999999999956631913101,
  412. 0.99999999999999999978315956551,
  413. 0.99999999999999999989157978275,
  414. 0.99999999999999999994578989138,
  415. 0.99999999999999999997289494569,
  416. 0.99999999999999999998644747284,
  417. 0.99999999999999999999322373642,
  418. 0.99999999999999999999661186821,
  419. 0.99999999999999999999830593411,
  420. 0.99999999999999999999915296705,
  421. 0.99999999999999999999957648353,
  422. 0.99999999999999999999978824176,
  423. 0.99999999999999999999989412088,
  424. 0.99999999999999999999994706044,
  425. 0.99999999999999999999997353022,
  426. 0.99999999999999999999998676511,
  427. 0.99999999999999999999999338256,
  428. 0.99999999999999999999999669128,
  429. 0.99999999999999999999999834564,
  430. 0.99999999999999999999999917282,
  431. 0.99999999999999999999999958641,
  432. 0.99999999999999999999999979320,
  433. 0.99999999999999999999999989660,
  434. 0.99999999999999999999999994830,
  435. 0.99999999999999999999999997415,
  436. 0.99999999999999999999999998708,
  437. 0.99999999999999999999999999354,
  438. 0.99999999999999999999999999677,
  439. 0.99999999999999999999999999838,
  440. 0.99999999999999999999999999919,
  441. 0.99999999999999999999999999960,
  442. 0.99999999999999999999999999980,
  443. 0.99999999999999999999999999990,
  444. 0.99999999999999999999999999995,
  445. 0.99999999999999999999999999997,
  446. 0.99999999999999999999999999999,
  447. 0.99999999999999999999999999999,
  448. 1.00000000000000000000000000000,
  449. 1.00000000000000000000000000000,
  450. 1.00000000000000000000000000000,
  451. };
  452.  
  453.  
  454. #define ETA_NEG_TABLE_NMAX  99
  455. #define ETA_NEG_TABLE_SIZE  50
  456. static double eta_neg_int_table[ETA_NEG_TABLE_SIZE] = {
  457.  0.25000000000000000000000000000,   /* eta(-1) */
  458. -0.12500000000000000000000000000,   /* eta(-3) */
  459.  0.25000000000000000000000000000,   /* ...    */
  460. -1.06250000000000000000000000000,
  461.  7.75000000000000000000000000000,
  462. -86.3750000000000000000000000000,
  463.  1365.25000000000000000000000000,
  464. -29049.0312500000000000000000000,
  465.  800572.750000000000000000000000,
  466. -2.7741322625000000000000000000e+7,
  467.  1.1805291302500000000000000000e+9,
  468. -6.0523980051687500000000000000e+10,
  469.  3.6794167785377500000000000000e+12,
  470. -2.6170760990658387500000000000e+14,
  471.  2.1531418140800295250000000000e+16,
  472. -2.0288775575173015930156250000e+18,
  473.  2.1708009902623770590275000000e+20,
  474. -2.6173826968455814932120125000e+22,
  475.  3.5324148876863877826668602500e+24,
  476. -5.3042033406864906641493838981e+26,
  477.  8.8138218364311576767253114668e+28,
  478. -1.6128065107490778547354654864e+31,
  479.  3.2355470001722734208527794569e+33,
  480. -7.0876727476537493198506645215e+35,
  481.  1.6890450341293965779175629389e+38,
  482. -4.3639690731216831157655651358e+40,
  483.  1.2185998827061261322605065672e+43,
  484. -3.6670584803153006180101262324e+45,
  485.  1.1859898526302099104271449748e+48,
  486. -4.1120769493584015047981746438e+50,
  487.  1.5249042436787620309090168687e+53,
  488. -6.0349693196941307074572991901e+55,
  489.  2.5437161764210695823197691519e+58,
  490. -1.1396923802632287851130360170e+61,
  491.  5.4180861064753979196802726455e+63,
  492. -2.7283654799994373847287197104e+66,
  493.  1.4529750514918543238511171663e+69,
  494. -8.1705519371067450079777183386e+71,
  495.  4.8445781606678367790247757259e+74,
  496. -3.0246694206649519336179448018e+77,
  497.  1.9858807961690493054169047970e+80,
  498. -1.3694474620720086994386818232e+83,
  499.  9.9070382984295807826303785989e+85,
  500. -7.5103780796592645925968460677e+88,
  501.  5.9598418264260880840077992227e+91,
  502. -4.9455988887500020399263196307e+94,
  503.  4.2873596927020241277675775935e+97,
  504. -3.8791952037716162900707994047e+100,
  505.  3.6600317773156342245401829308e+103,
  506. -3.5978775704117283875784869570e+106    /* eta(-99)  */
  507. };
  508.  
  509.  
  510. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  511.  
  512.  
  513. int gsl_sf_hzeta_e(const double s, const double q, gsl_sf_result * result)
  514. {
  515.   /* CHECK_POINTER(result) */
  516.  
  517.   if(s <= 1.0 || q <= 0.0) {
  518.     DOMAIN_ERROR(result);
  519.   }
  520.   else {
  521.     const double max_bits = 54.0;
  522.     const double ln_term0 = -s * log(q);  
  523.  
  524.     if(ln_term0 < GSL_LOG_DBL_MIN + 1.0) {
  525.       UNDERFLOW_ERROR(result);
  526.     }
  527.     else if(ln_term0 > GSL_LOG_DBL_MAX - 1.0) {
  528.       OVERFLOW_ERROR (result);
  529.     }
  530.     else if((s > max_bits && q < 1.0) || (s > 0.5*max_bits && q < 0.25)) {
  531.       result->val = pow(q, -s);
  532.       result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  533.       return GSL_SUCCESS;
  534.     }
  535.     else if(s > 0.5*max_bits && q < 1.0) {
  536.       const double p1 = pow(q, -s);
  537.       const double p2 = pow(q/(1.0+q), s);
  538.       const double p3 = pow(q/(2.0+q), s);
  539.       result->val = p1 * (1.0 + p2 + p3);
  540.       result->err = GSL_DBL_EPSILON * (0.5*s + 2.0) * fabs(result->val);
  541.       return GSL_SUCCESS;
  542.     }
  543.     else {
  544.       /* Euler-Maclaurin summation formula 
  545.        * [Moshier, p. 400, with several typo corrections]
  546.        */
  547.       const int jmax = 12;
  548.       const int kmax = 10;
  549.       int j, k;
  550.       const double pmax  = pow(kmax + q, -s);
  551.       double scp = s;
  552.       double pcp = pmax / (kmax + q);
  553.       double ans = pmax*((kmax+q)/(s-1.0) + 0.5);
  554.  
  555.       for(k=0; k<kmax; k++) {
  556.         ans += pow(k + q, -s);
  557.       }
  558.  
  559.       for(j=0; j<=jmax; j++) {
  560.         double delta = hzeta_c[j+1] * scp * pcp;
  561.         ans += delta;
  562.         if(fabs(delta/ans) < 0.5*GSL_DBL_EPSILON) break;
  563.         scp *= (s+2*j+1)*(s+2*j+2);
  564.         pcp /= (kmax + q)*(kmax + q);
  565.       }
  566.  
  567.       result->val = ans;
  568.       result->err = 2.0 * (jmax + 1.0) * GSL_DBL_EPSILON * fabs(ans);
  569.       return GSL_SUCCESS;
  570.     }
  571.   }
  572. }
  573.  
  574.  
  575. int gsl_sf_zeta_e(const double s, gsl_sf_result * result)
  576. {
  577.   /* CHECK_POINTER(result) */
  578.  
  579.   if(s == 1.0) {
  580.     DOMAIN_ERROR(result);
  581.   }
  582.   else if(s >= 0.0) {
  583.     return riemann_zeta_sgt0(s, result);
  584.   }
  585.   else {
  586.     /* reflection formula, [Abramowitz+Stegun, 23.2.5] */
  587.  
  588.     gsl_sf_result zeta_one_minus_s;
  589.     const int stat_zoms = riemann_zeta1m_slt0(s, &zeta_one_minus_s);
  590.     const double sin_term = sin(0.5*M_PI*s)/M_PI;
  591.  
  592.     if(sin_term == 0.0) {
  593.       result->val = 0.0;
  594.       result->err = 0.0;
  595.       return GSL_SUCCESS;
  596.     }
  597.     else if(s > -170) {
  598.       /* We have to be careful about losing digits
  599.        * in calculating pow(2 Pi, s). The gamma
  600.        * function is fine because we were careful
  601.        * with that implementation.
  602.        * We keep an array of (2 Pi)^(10 n).
  603.        */
  604.       const double twopi_pow[18] = { 1.0,
  605.                                      9.589560061550901348e+007,
  606.                                      9.195966217409212684e+015,
  607.                                      8.818527036583869903e+023,
  608.                                      8.456579467173150313e+031,
  609.                                      8.109487671573504384e+039,
  610.                                      7.776641909496069036e+047,
  611.                                      7.457457466828644277e+055,
  612.                                      7.151373628461452286e+063,
  613.                                      6.857852693272229709e+071,
  614.                                      6.576379029540265771e+079,
  615.                                      6.306458169130020789e+087,
  616.                                      6.047615938853066678e+095,
  617.                                      5.799397627482402614e+103,
  618.                                      5.561367186955830005e+111,
  619.                                      5.333106466365131227e+119,
  620.                      5.114214477385391780e+127,
  621.                      4.904306689854036836e+135
  622.                                     };
  623.       const int n = floor((-s)/10.0);
  624.       const double fs = s + 10.0*n;
  625.       const double p = pow(2.0*M_PI, fs) / twopi_pow[n];
  626.  
  627.       gsl_sf_result g;
  628.       const int stat_g = gsl_sf_gamma_e(1.0-s, &g);
  629.       result->val  = p * g.val * sin_term * zeta_one_minus_s.val;
  630.       result->err  = fabs(p * g.val * sin_term) * zeta_one_minus_s.err;
  631.       result->err += fabs(p * sin_term * zeta_one_minus_s.val) * g.err;
  632.       result->err += GSL_DBL_EPSILON * (fabs(s)+2.0) * fabs(result->val);
  633.       return GSL_ERROR_SELECT_2(stat_g, stat_zoms);
  634.     }
  635.     else {
  636.       /* The actual zeta function may or may not
  637.        * overflow here. But we have no easy way
  638.        * to calculate it when the prefactor(s)
  639.        * overflow. Trying to use log's and exp
  640.        * is no good because we loose a couple
  641.        * digits to the exp error amplification.
  642.        * When we gather a little more patience,
  643.        * we can implement something here. Until
  644.        * then just give up.
  645.        */
  646.       OVERFLOW_ERROR(result);
  647.     }
  648.   }
  649. }
  650.  
  651.  
  652. int gsl_sf_zeta_int_e(const int n, gsl_sf_result * result)
  653. {
  654.   /* CHECK_POINTER(result) */
  655.  
  656.   if(n < 0) {
  657.     if(!GSL_IS_ODD(n)) {
  658.       result->val = 0.0; /* exactly zero at even negative integers */
  659.       result->err = 0.0;
  660.       return GSL_SUCCESS;
  661.     }
  662.     else if(n > -ZETA_NEG_TABLE_NMAX) {
  663.       result->val = zeta_neg_int_table[-(n+1)/2];
  664.       result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  665.       return GSL_SUCCESS;
  666.     }
  667.     else {
  668.       return gsl_sf_zeta_e((double)n, result);
  669.     }
  670.   }
  671.   else if(n == 1){
  672.     DOMAIN_ERROR(result);
  673.   }
  674.   else if(n <= ZETA_POS_TABLE_NMAX){
  675.     result->val = zeta_pos_int_table[n];
  676.     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  677.     return GSL_SUCCESS;
  678.   }
  679.   else {
  680.     result->val = 1.0;
  681.     result->err = GSL_DBL_EPSILON;
  682.     return GSL_SUCCESS;
  683.   }
  684. }
  685.  
  686.  
  687. int gsl_sf_eta_int_e(int n, gsl_sf_result * result)
  688. {
  689.   if(n > ETA_POS_TABLE_NMAX) {
  690.     result->val = 1.0;
  691.     result->err = GSL_DBL_EPSILON;
  692.     return GSL_SUCCESS;
  693.   }
  694.   else if(n >= 0) {
  695.     result->val = eta_pos_int_table[n];
  696.     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  697.     return GSL_SUCCESS;
  698.   }
  699.   else {
  700.     /* n < 0 */
  701.  
  702.     if(!GSL_IS_ODD(n)) {
  703.       /* exactly zero at even negative integers */
  704.       result->val = 0.0;
  705.       result->err = 0.0;
  706.       return GSL_SUCCESS;
  707.     }
  708.     else if(n > -ETA_NEG_TABLE_NMAX) {
  709.       result->val = eta_neg_int_table[-(n+1)/2];
  710.       result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  711.       return GSL_SUCCESS;
  712.     }
  713.     else {
  714.       gsl_sf_result z;
  715.       gsl_sf_result p;
  716.       int stat_z = gsl_sf_zeta_int_e(n, &z);
  717.       int stat_p = gsl_sf_exp_e((1.0-n)*M_LN2, &p);
  718.       int stat_m = gsl_sf_multiply_e(-p.val, z.val, result);
  719.       result->err  = fabs(p.err * (M_LN2*(1.0-n)) * z.val) + z.err * fabs(p.val);
  720.       result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  721.       return GSL_ERROR_SELECT_3(stat_m, stat_p, stat_z);
  722.     }
  723.   }
  724. }
  725.  
  726.  
  727. int gsl_sf_eta_e(const double s, gsl_sf_result * result)
  728. {
  729.   /* CHECK_POINTER(result) */
  730.  
  731.   if(s > 100.0) {
  732.     result->val = 1.0;
  733.     result->err = GSL_DBL_EPSILON;
  734.     return GSL_SUCCESS;
  735.   }
  736.   else if(fabs(s-1.0) < 10.0*GSL_ROOT5_DBL_EPSILON) {
  737.     double del = s-1.0;
  738.     double c0  = M_LN2;
  739.     double c1  = M_LN2 * (M_EULER - 0.5*M_LN2);
  740.     double c2  = -0.0326862962794492996;
  741.     double c3  =  0.0015689917054155150;
  742.     double c4  =  0.00074987242112047532;
  743.     result->val = c0 + del * (c1 + del * (c2 + del * (c3 + del * c4)));
  744.     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  745.     return GSL_SUCCESS;
  746.   }
  747.   else {
  748.     gsl_sf_result z;
  749.     gsl_sf_result p;
  750.     int stat_z = gsl_sf_zeta_e(s, &z);
  751.     int stat_p = gsl_sf_exp_e((1.0-s)*M_LN2, &p);
  752.     int stat_m = gsl_sf_multiply_e(1.0-p.val, z.val, result);
  753.     result->err  = fabs(p.err * (M_LN2*(1.0-s)) * z.val) + z.err * fabs(p.val);
  754.     result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  755.     return GSL_ERROR_SELECT_3(stat_m, stat_p, stat_z);
  756.   }
  757. }
  758.  
  759.  
  760. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  761.  
  762. #include "eval.h"
  763.  
  764. double gsl_sf_zeta(const double s)
  765. {
  766.   EVAL_RESULT(gsl_sf_zeta_e(s, &result));
  767. }
  768.  
  769. double gsl_sf_hzeta(const double s, const double a)
  770. {
  771.   EVAL_RESULT(gsl_sf_hzeta_e(s, a, &result));
  772. }
  773.  
  774. double gsl_sf_zeta_int(const int s)
  775. {
  776.   EVAL_RESULT(gsl_sf_zeta_int_e(s, &result));
  777. }
  778.  
  779. double gsl_sf_eta_int(const int s)
  780. {
  781.   EVAL_RESULT(gsl_sf_eta_int_e(s, &result));
  782. }
  783.  
  784. double gsl_sf_eta(const double s)
  785. {
  786.   EVAL_RESULT(gsl_sf_eta_e(s, &result));
  787. }
  788.